﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TuanAnhPersonalWeb.Models;

namespace TuanAnhPersonalWeb.Areas.Admin.Controllers
{
    [Authorize(Roles = "admin,project")]
    public class CommentController : Controller
    {
        private TuanAnhEntities db = new TuanAnhEntities();

        //
        // GET: /Admin/Comment/

        public ViewResult Index()
        {
            var tuananh__comment = db.TUANANH__COMMENT.Include("TUANANH__PROJECT").OrderByDescending(e=>e.DATE);
            return View(tuananh__comment.ToList());
        }

        //
        // GET: /Admin/Comment/Details/5

        public ViewResult Details(int id,DateTime date)
        {
            TUANANH__COMMENT tuananh__comment = db.TUANANH__COMMENT.Single(t => t.PROJECT_ID == id && t.DATE==date);
            return View(tuananh__comment);
        }

        //
        // GET: /Admin/Comment/Create

        public ActionResult Create()
        {
            ViewBag.PROJECT_ID = new SelectList(db.TUANANH__PROJECT, "PROJECT_ID", "PROJECT_CONTENT");
            return View();
        } 

        //
        // POST: /Admin/Comment/Create

        [HttpPost]
        public ActionResult Create(TUANANH__COMMENT tuananh__comment)
        {
            if (ModelState.IsValid)
            {
                db.TUANANH__COMMENT.AddObject(tuananh__comment);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.PROJECT_ID = new SelectList(db.TUANANH__PROJECT, "PROJECT_ID", "PROJECT_CONTENT", tuananh__comment.PROJECT_ID);
            return View(tuananh__comment);
        }
        
        //
        // GET: /Admin/Comment/Edit/5

        public ActionResult Edit(int id, DateTime date)
        {
            TUANANH__COMMENT tuananh__comment = db.TUANANH__COMMENT.Single(t => t.PROJECT_ID == id && t.DATE == date);
            ViewBag.PROJECT_ID = new SelectList(db.TUANANH__PROJECT, "PROJECT_ID", "PROJECT_CONTENT", tuananh__comment.PROJECT_ID);
            return View(tuananh__comment);
        }

        //
        // POST: /Admin/Comment/Edit/5

        [HttpPost]
        public ActionResult Edit(TUANANH__COMMENT tuananh__comment)
        {
            if (ModelState.IsValid)
            {
                db.TUANANH__COMMENT.Attach(tuananh__comment);
                db.ObjectStateManager.ChangeObjectState(tuananh__comment, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.PROJECT_ID = new SelectList(db.TUANANH__PROJECT, "PROJECT_ID", "PROJECT_CONTENT", tuananh__comment.PROJECT_ID);
            return View(tuananh__comment);
        }

        //
        // GET: /Admin/Comment/Delete/5

        public ActionResult Delete(int id, DateTime date)
        {
            TUANANH__COMMENT tuananh__comment = db.TUANANH__COMMENT.Single(t => t.PROJECT_ID == id && t.DATE == date);
            return View(tuananh__comment);
        }

        //
        // POST: /Admin/Comment/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id, DateTime date)
        {            
            TUANANH__COMMENT tuananh__comment = db.TUANANH__COMMENT.Single(t => t.PROJECT_ID == id && t.DATE == date);
            db.TUANANH__COMMENT.DeleteObject(tuananh__comment);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}